package com.winspread.ui.edittext

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import com.winspread.ui.R

class IPEditText(context: Context?, attributeSet: AttributeSet?) :
    LinearLayout(context, attributeSet) {
    private val firIPEdit: EditText
    private val secIPEdit: EditText
    private val thirIPEdit: EditText
    private val fourIPEdit: EditText
    private var firstIP = ""
    private var secondIP = ""
    private var thirdIP = ""
    private var fourthIP = ""
    fun setIPEditTextListener(context: Context?) {
        //设置第一个IP字段的事件监听
        firIPEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.toString().trim { it <= ' ' } == ".") {
                    firstIP = ""
                    return
                }
                if (s.length > 2 || s.toString().trim { it <= ' ' }.contains(".")) {
                    firstIP = if (s.toString().trim { it <= ' ' }.contains(".")) {
                        s.toString().trim { it <= ' ' }.substring(0, s.length - 1)
                    } else {
                        s.toString().trim { it <= ' ' }
                    }
                    if (firstIP.toInt() > 255) {
                        firstIP = "255"
                    }
                    secIPEdit.isFocusable = true
                    secIPEdit.requestFocus()
                } else {
                    firstIP = s.toString().trim { it <= ' ' }
                }
            }

            override fun afterTextChanged(s: Editable) {
                firIPEdit.removeTextChangedListener(this)
                firIPEdit.setText(firstIP)
                firIPEdit.setSelection(firIPEdit.length())
                firIPEdit.addTextChangedListener(this)
            }
        })
        //设置第二个IP字段的事件监听
        secIPEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                //若长度为0，返回到上一个文本编辑框
                if (s.toString().length == 0) {
                    firIPEdit.isFocusable = true
                    firIPEdit.requestFocus()
                }
                if (s.toString().trim { it <= ' ' } == ".") {
                    secondIP = ""
                    return
                }
                if (s.length > 2 || s.toString().trim { it <= ' ' }.contains(".")) {
                    secondIP = if (s.toString().trim { it <= ' ' }.contains(".")) {
                        s.toString().trim { it <= ' ' }.substring(0, s.length - 1)
                    } else {
                        s.toString().trim { it <= ' ' }
                    }
                    if (secondIP.toInt() > 255) {
                        secondIP = "255"
                    }
                    thirIPEdit.isFocusable = true
                    thirIPEdit.requestFocus()
                } else {
                    secondIP = s.toString().trim { it <= ' ' }
                }
            }

            override fun afterTextChanged(s: Editable) {
                secIPEdit.removeTextChangedListener(this)
                secIPEdit.setText(secondIP)
                secIPEdit.setSelection(secondIP.length)
                secIPEdit.addTextChangedListener(this)
            }
        })

        //设置第三个IP字段的事件监听
        thirIPEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.toString().length == 0) {
                    secIPEdit.isFocusable = true
                    secIPEdit.requestFocus()
                }
                if (s.toString().trim { it <= ' ' } == ".") {
                    thirdIP = ""
                    return
                }
                if (s.length > 2 || s.toString().trim { it <= ' ' }.contains(".")) {
                    thirdIP = if (s.toString().trim { it <= ' ' }.contains(".")) {
                        s.toString().trim { it <= ' ' }.substring(0, s.length - 1)
                    } else {
                        s.toString().trim { it <= ' ' }
                    }
                    if (thirdIP.toInt() > 255) {
                        thirdIP = "255"
                    }
                    fourIPEdit.isFocusable = true
                    fourIPEdit.requestFocus()
                } else {
                    thirdIP = s.toString().trim { it <= ' ' }
                }
            }

            override fun afterTextChanged(s: Editable) {
                thirIPEdit.removeTextChangedListener(this)
                thirIPEdit.setText(thirdIP)
                thirIPEdit.setSelection(thirdIP.length)
                thirIPEdit.addTextChangedListener(this)
            }
        })

        //设置第四个IP字段的事件监听
        fourIPEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.toString().length == 0) {
                    thirIPEdit.isFocusable = true
                    thirIPEdit.requestFocus()
                }
                if (s.toString().trim { it <= ' ' } == ".") {
                    fourthIP = ""
                    return
                }
                if (s.length > 2 || s.toString().trim { it <= ' ' }.contains(".")) {
                    fourthIP = if (s.toString().trim { it <= ' ' }.contains(".")) {
                        s.toString().trim { it <= ' ' }.substring(0, s.length - 1)
                    } else {
                        s.toString().trim { it <= ' ' }
                    }
                    if (fourthIP.toInt() > 255) {
                        fourthIP = "255"
                    }
                } else {
                    fourthIP = s.toString().trim { it <= ' ' }
                }
            }

            override fun afterTextChanged(s: Editable) {
                fourIPEdit.removeTextChangedListener(this)
                fourIPEdit.setText(fourthIP)
                fourIPEdit.setSelection(fourthIP.length)
                fourIPEdit.addTextChangedListener(this)
            }
        })


    }
    /**
     * 返回整个ip地址
     *
     * @return
     */
    /**
     * 本地读取的ip地址显示至界面
     *
     * @param ipText
     */
    var ipText: String?
        get() = if (TextUtils.isEmpty(firstIP) || TextUtils.isEmpty(secondIP)
            || TextUtils.isEmpty(thirdIP) || TextUtils.isEmpty(fourthIP)
        ) {
            null
        } else "$firstIP.$secondIP.$thirdIP.$fourthIP"
        set(ipText) {
            if (TextUtils.isEmpty(ipText) || ipText == null) {
                return
            }
            var temp: Array<String?>? = null
            temp = ipText.split("\\.".toRegex()).toTypedArray()
            if (temp != null) {
                firIPEdit.setText(temp[0])
                secIPEdit.setText(temp[1])
                thirIPEdit.setText(temp[2])
                fourIPEdit.setText(temp[3])
            }
        }

    init {
        LayoutInflater.from(context).inflate(R.layout.view_ip_text_layout, this)
        firIPEdit = findViewById(R.id.Fist_Text)
        secIPEdit = findViewById(R.id.Second_Text)
        thirIPEdit = findViewById(R.id.Third_Text)
        fourIPEdit = findViewById(R.id.Four_Text)
        setIPEditTextListener(context)
    }
}